package com.aerospace.example.hello;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.stub.StreamObserver;

import java.util.Iterator;

/**
 * @ClassName HelloClient
 * @Description
 * @Author yuhang11
 * @Date 2021/12/7 7:13 下午
 * @Version 1.0
 */
public class HelloClient {
    public static void main(String[] args) {
        //建立channel，线程安全，可重用
        ManagedChannel channel = ManagedChannelBuilder.forTarget("localhost:50051").usePlaintext().build();
        try{
            //请求
            simpleRequest(channel);
//            serverStreamRequest(channel);
//            clientStreamRequest(channel);
//            biStreamRequest(channel);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private static void simpleRequest(ManagedChannel channel) {
        //定义同步阻塞的stub
        GreeterGrpc.GreeterBlockingStub blockingStub = GreeterGrpc.newBlockingStub(channel);
        HelloRequest helloRequest = HelloRequest.newBuilder().setName("liuyuhang").build();
        HelloReply helloReply = blockingStub.simpleHello(helloRequest);
        System.out.println("simple--"+helloReply.getMessage());
    }

    //服务端流式
    private static void serverStreamRequest(ManagedChannel channel) {
        //定义同步阻塞的stub
        GreeterGrpc.GreeterBlockingStub blockingStub = GreeterGrpc.newBlockingStub(channel);
        HelloRequest helloRequest = HelloRequest.newBuilder().setName("liuyuhang").build();

        //请求并处理返回结果
        Iterator<HelloReply> it = blockingStub.serverStreamHello(helloRequest);
        while(it.hasNext()){
            System.out.println("serverStream--"+it.next());
        }
//        channel.shutdown();
    }
    //客户端流式
    private static void clientStreamRequest(final ManagedChannel channel) throws InterruptedException {
        //定义异步的stub
        GreeterGrpc.GreeterStub asyncStub = GreeterGrpc.newStub(channel);

        //结果处理
        StreamObserver<HelloReply> responseObserver = new StreamObserver<HelloReply>() {
            @Override
            public void onNext(HelloReply helloReply) {
                System.out.println("clientStream--"+helloReply);
            }

            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onCompleted() {

//                channel.shutdown();
            }
        };
        StreamObserver<HelloRequest> clientStreamObserver = asyncStub.clientStreamHello(responseObserver);
        clientStreamObserver.onNext(HelloRequest.newBuilder().setName("liuyuhang1").build());
        clientStreamObserver.onNext(HelloRequest.newBuilder().setName("liuyuhang2").build());
        clientStreamObserver.onCompleted();
        //异步等待结果，所以sleep 一秒
        Thread.sleep(1000);
    }
    //双向流式
    private static void biStreamRequest(ManagedChannel channel) throws InterruptedException {
        //定义异步的stub
        GreeterGrpc.GreeterStub asyncStub = GreeterGrpc.newStub(channel);

        StreamObserver<HelloReply> responseObserver = new StreamObserver<HelloReply>() {
            @Override
            public void onNext(HelloReply helloReply) {
                System.out.println("biStream--" + helloReply);
            }

            @Override
            public void onError(Throwable throwable) {
            }

            @Override
            public void onCompleted() {
            }
        };
        StreamObserver<HelloRequest> biStreamObserver = asyncStub.biStreamHello(responseObserver);
        biStreamObserver.onNext(HelloRequest.newBuilder().setName("liuyuhang1").build());
        biStreamObserver.onNext(HelloRequest.newBuilder().setName("liuyuhang2").build());
        biStreamObserver.onCompleted();
        //由于是异步获得结果，所以sleep一秒
        Thread.sleep(1000);
    }
}
